package com.atguigu.tingshu.handler;

import com.atguigu.tingshu.model.CDCEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

@Component
@CanalTable("album_info")
public class AlbumInfoCdcHandler implements EntryHandler<CDCEntity> {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void insert(CDCEntity cdcEntity) {
        EntryHandler.super.insert(cdcEntity);
    }

    @Override
    public void delete(CDCEntity cdcEntity) {
        EntryHandler.super.delete(cdcEntity);
    }

    @Override
    public void update(CDCEntity before, CDCEntity after) {
        System.out.println("before:\t" +before.getId());
        // 相当于获取对应的专辑id
        System.out.println("after:\t" +after.getId());
        // 删除缓存数据
        String key = "album:[" +after.getId()+ "]";
        try {
            redisTemplate.delete("album:" + after.getId());
        } catch (Exception e) {
            //  如果失败借助mq重试
            throw new RuntimeException(e);
        }
    }
}
